Page({
  data: {
    // 封面图文件列表
    coverFileList: [
      
    ],
    // 标签数据
    tags: ["鸡蛋", "盐", "白糖", "水", "青椒", "淀粉", "醋", "冰糖", "黄油"],
    // 用料行数据
    ingredientRows: [
      { material: "", unit: "" }
    ],
    // 菜谱标题
    recipeTitle: '',
    // 步骤数据
    steps: [
      { desc: '', images: [] }
    ],
    // 烹饪时间选项
    cookTimeOptions: ['15分钟左右', '15-30分钟', '30-60分钟', '1小时以上'],
    selectedCookTime: '15分钟左右',
    // 烹饪难度选项
    cookDifficultyOptions: ['零厨艺', '容易做', '有点挑战', '非常难'],
    selectedCookDifficulty: '零厨艺',
    // 推荐分类
    selectedCategory: '',
  },

  // 页面加载
  onLoad() {
    console.log("页面加载完成");
  },

  // 页面显示
  onShow() {
    // 页面显示时检查是否有分类选择更新
    // 这个逻辑会在从分类选择页面返回时触发
  },

  // 封面图上传成功回调
  onCoverAfterRead(e) {
    this.setData({
      coverFileList: e.detail.fileList
    });
    wx.showToast({
      title: '封面图上传成功',
      icon: 'success'
    });
  },

  // 封面图删除回调
  onCoverDelete(e) {
    this.setData({
      coverFileList: []
    });
    wx.showToast({
      title: '封面图已删除，可重新上传',
      icon: 'none'
    });
  },

  // 标题输入事件
  onTitleInput(e) {
    this.setData({
      recipeTitle: e.detail.value
    });
  },

  // 标签点击事件
  onTagClick(e) {
    const tag = e.currentTarget.dataset.tag;
    wx.showToast({
      title: `选择了${tag}`,
      icon: 'none'
    });
  },

  // 材料输入框事件
  onMaterialInput(e, index) {
    const { ingredientRows } = this.data;
    ingredientRows[index].material = e.detail.value;
    this.setData({ ingredientRows });
  },

  // 单位输入框事件
  onUnitInput(e, index) {
    const { ingredientRows } = this.data;
    ingredientRows[index].unit = e.detail.value;
    this.setData({ ingredientRows });
  },

  // 新增一行
  onAddRow() {
    const { ingredientRows } = this.data;
    ingredientRows.push({ material: "", unit: "" });
    this.setData({ ingredientRows });
  },

  // 删除一行
  onDeleteRow(e) {
    const { index } = e.currentTarget.dataset;
    const { ingredientRows } = this.data;
    if (ingredientRows.length > 1) {
      ingredientRows.splice(index, 1);
      this.setData({ ingredientRows });
    }
  },

  // 步骤图片上传回调
  onStepImageRead(e) {
    const index = e.currentTarget.dataset.index;
    const { steps } = this.data;
    steps[index].images = e.detail.fileList;
    this.setData({ steps });
    wx.showToast({ 
      title: '步骤图上传成功', 
      icon: 'success' 
    });
  },

  // 步骤图片删除回调
  onStepImageDelete(e) {
    const index = e.currentTarget.dataset.index;
    const { steps } = this.data;
    steps[index].images = [];
    this.setData({ steps });
    wx.showToast({ 
      title: '步骤图已删除', 
      icon: 'none' 
    });
  },

  // 步骤说明输入
  onStepDescInput(e, index) {
    const { steps } = this.data;
    steps[index].desc = e.detail.value;
    this.setData({ steps });
  },

  // 新增步骤
  onAddStep() {
    const { steps } = this.data;
    steps.push({ desc: '', images: [] });
    this.setData({ steps });
  },

  // 删除步骤
  onDeleteStep(e) {
    const { index } = e.currentTarget.dataset;
    const { steps } = this.data;
    if (steps.length > 1) {
      steps.splice(index, 1);
      this.setData({ steps });
    }
  },

  // 烹饪时间选择
  onSelectCookTime(e) {
    const selectedValue = e.currentTarget.dataset.value;
    this.setData({ 
      selectedCookTime: selectedValue 
    });
  },

  // 烹饪难度选择
  onSelectCookDifficulty(e) {
    const selectedValue = e.currentTarget.dataset.value;
    this.setData({ 
      selectedCookDifficulty: selectedValue 
    });
  },

  // 跳转分类选择
  onGoToCategory() {
    wx.navigateTo({
      url: `/transmit/pages/transmitselect/transmitselect`
    });
  },

  // 发布菜谱
  onPublishRecipe() {
    // 验证必填项
    if (!this.data.recipeTitle.trim()) {
      wx.showToast({ 
        title: '请输入菜谱标题', 
        icon: 'none' 
      });
      return;
    }

    if (!this.data.coverFileList.length) {
      wx.showToast({ 
        title: '请上传封面图', 
        icon: 'none' 
      });
      return;
    }

    // 验证用料是否填写
    const hasIngredients = this.data.ingredientRows.some(row => 
      row.material.trim() && row.unit.trim()
    );
    if (!hasIngredients) {
      wx.showToast({ 
        title: '请填写至少一个用料', 
        icon: 'none' 
      });
      return;
    }

    // 验证步骤是否填写
    const hasSteps = this.data.steps.some(step => step.desc.trim());
    if (!hasSteps) {
      wx.showToast({ 
        title: '请填写至少一个步骤', 
        icon: 'none' 
      });
      return;
    }

    // 验证分类是否选择
    if (!this.data.selectedCategory) {
      wx.showToast({ 
        title: '请选择菜谱分类', 
        icon: 'none' 
      });
      return;
    }

    wx.showModal({
      title: '确认发布',
      content: '确定要发布这个菜谱吗？发布后将无法修改。',
      success: (res) => {
        if (res.confirm) {
          // 显示加载提示
          wx.showLoading({
            title: '发布中...',
            mask: true
          });

          // 模拟发布过程
          setTimeout(() => {
            wx.hideLoading();
            wx.showToast({ 
              title: '菜谱发布成功', 
              icon: 'success',
              duration: 2000
            });
            
            // 发布成功后返回上一页
            setTimeout(() => {
              wx.navigateBack();
            }, 2000);
          }, 1500);
        }
      }
    });
  },

  // 删除菜谱
  onDeleteRecipe() {
    wx.showModal({
      title: '确认删除',
      content: '确定要删除这个菜谱吗？删除后无法恢复。',
      success: (res) => {
        if (res.confirm) {
          // 显示加载提示
          wx.showLoading({
            title: '删除中...',
            mask: true
          });

          // 模拟删除过程
          setTimeout(() => {
            wx.hideLoading();
            wx.showToast({ 
              title: '菜谱已删除', 
              icon: 'success',
              duration: 2000
            });
            
            // 删除成功后返回上一页
            setTimeout(() => {
              wx.navigateBack();
            }, 2000);
          }, 1000);
        }
      }
    });
  },

  // 导航栏事件
  onClickLeft() {
    wx.navigateBack();
  },

  onClickRight() {
    wx.showActionSheet({
      itemList: ['保存草稿', '预览', '分享', '更多设置'],
      success: (res) => {
        const actions = ['保存草稿', '预览', '分享', '更多设置'];
        wx.showToast({ 
          title: `选择了${actions[res.tapIndex]}`, 
          icon: 'none' 
        });
      }
    });
  }
});